package cn.yshujia.service.impl;

import cn.yshujia.domain.dto.PageDTO;
import cn.yshujia.domain.entity.Resources;
import cn.yshujia.domain.vo.PageVO;
import cn.yshujia.domain.vo.ResourcesVO;
import cn.yshujia.enums.MinioFolder;
import cn.yshujia.mapper.ResourcesMapper;
import cn.yshujia.repository.ResourcesRepository;
import cn.yshujia.utils.IDUtils;
import cn.yshujia.utils.MinioUtils;
import cn.yshujia.utils.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author YShuJia
 * @since 2024/4/23
 */
@Service
public class ResourcesServiceImpl extends ServiceImpl<ResourcesMapper, Resources> implements IService<Resources> {
	
	@Resource
	ResourcesMapper mapper;
	
	@Resource
	ResourcesRepository repository;
	
	public PageVO<ResourcesVO> selectPage(PageDTO dto) {
		return repository.selectPageByLabelId(dto);
	}
	
	public PageVO<ResourcesVO> selectIconPage(PageDTO dto) {
		dto.getParam().put("labelId", 10000001L);
		return repository.selectPageByLabelId(dto);
	}
	
	public PageVO<ResourcesVO> selectWallPage(PageDTO dto) {
		dto.getParam().put("labelId", 10000002L);
		return repository.selectPageByLabelId(dto);
	}
	
	public PageVO<ResourcesVO> adminPage(PageDTO dto) {
		QueryWrapper<Resources> wrapper = PageUtils.createResourcesWrapper(dto);
		IPage<Resources> page = mapper.selectPage(new Page<>(dto.getCurrent(), dto.getSize()), wrapper);
		// 创建VO集合
		if (!ObjectUtils.isEmpty(page) && !ObjectUtils.isEmpty(page.getRecords())) {
			return PageUtils.getPage(dto, repository.createVOList(page.getRecords()), page.getTotal());
		}
		return new PageVO<>();
	}
	
	@Transactional (rollbackFor = {Exception.class})
	public Boolean insert(MultipartFile[] files, Resources resources) {
		List<Resources> resourcesList = new ArrayList<>();
		Map<String, Double> map = MinioUtils.uploadGetUrlAndSize(files, MinioFolder.RESOURCES);
		Set<String> urls = map.keySet();
		for (String url : urls) {
			Resources resource = new Resources();
			resource.setId(IDUtils.getId());
			resource.setUrl(url);
			resource.setSize(map.get(url));
			resource.setName(resource.getUrl().substring(resource.getUrl().lastIndexOf("/") + 1));
			resource.setLabelId(resources.getLabelId());
			resource.setRemark(resources.getRemark());
			resourcesList.add(resource);
		}
		List<BatchResult> n = mapper.insert(resourcesList);
		if (!n.isEmpty()) {
			repository.del();
			return true;
		}
		MinioUtils.delete(urls);
		return false;
	}
	
	@Transactional (rollbackFor = {Exception.class})
	public Boolean update(Resources resources) {
		int n = mapper.updateById(resources);
		if (n > 0) {
			repository.del();
		}
		return n > 0;
	}
	
	@Transactional (rollbackFor = {Exception.class})
	public Boolean remove(List<Long> ids) {
		if (ObjectUtils.isEmpty(ids)) {
			return false;
		}
		List<Resources> resources = mapper.selectBatchIds(ids);
		int n = mapper.deleteByIds(ids);
		if (n > 0) {
			MinioUtils.delete(resources.stream().map(Resources::getUrl).toList());
			repository.del();
		}
		return n > 0;
	}
}
